package org.jackysoft.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.Collection;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.controller.base.AbstractController;
import org.jackysoft.entity.OfficeDocument;
import org.jackysoft.entity.OfficeTemplate;
import org.jackysoft.service.OfficeDocumentService;
import org.jackysoft.service.OfficeTemplateService;
import org.jackysoft.util.FileDownload;
import org.jackysoft.util.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhuozhengsoft.pageoffice.BorderStyleType;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.wordwriter.WordDocument;

/**
 * 该类用来协助保存处理在线office文件的 操作 其操作形式主要依赖于所用的在线office 控件 现在我们依赖的是科翰公司的SOAOffice,所以
 * 使用了SOAOffice的API
 * */

@Controller
@RequestMapping("/officetemplate")
public class OfficeTemplateController extends AbstractController<OfficeTemplate> {

	private static final Log logger = LogFactory.getLog(OfficeTemplateController.class);
	@Resource
	private OfficeTemplateService service;

	@Resource
	private OfficeDocumentService docService;

	/**
	 * 显示模板内容并 生成控件的saveurl;
	 */
	@RequestMapping("/viewtpl/{id}")
	public void viewtpl(
			@PathVariable  String id,		
			HttpServletResponse response
			
			) {
		
		
		FileDownload fd;
		try {
			fd = service.download(id);
			response.reset();  
			response.setHeader("Content-Disposition", "attachment; filename=\"" + fd.getTitle() + "\"");  
			response.addHeader("Content-Length", "" +fd.getDataSize());  
			response.setContentType("application/octet-stream;charset=UTF-8");  
			BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());  
			IOUtils.copyLarge(fd.getInputStream(), outputStream);
			outputStream.flush();  
			outputStream.close(); 
		} catch (IOException e) {
			logger.error(e);
		}
		
		
		
	}

	

	

	/**
	 * <dd>列出模板选择如果该工作流</dd> <dd>已经有了OFFICE模板实例,那么就</dd> <dd>直接打开,否则,列出模板让大家选</dd>
	 * <dd>择
	 */
	@RequestMapping("/preselect/{refid}")
	public String preselect(
			@PathVariable long refid,
			@ModelAttribute String saveUrl,
			@ModelAttribute OfficeDocument office,
			@ModelAttribute Collection<OfficeTemplate> beans
			) throws Exception {

		saveUrl = "admin/office/office-manager!savedoc?workflowId="+ refid;

		office = docService.findByRefid(refid);
		if (office == null) {
			beans = service.findAll();
			return "prepare";
		}
		return "opendoc";
	}

	
	public String preview(long workflowId,String openUrl, OpenModeType openModeType,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + request.getContextPath()
				+ "/";
		WordDocument doc = new WordDocument();

		PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);

		poCtrl1.setServerPage(basePath + "poserver.do"); // 此行必须
		// 设置界面样式
		poCtrl1.setCaption("庆华公司文件预览");
		poCtrl1.setBorderStyle(BorderStyleType.BorderThin);
		// 添加自定义工具条按钮
		boolean readonly = true;
		// 隐藏工具栏
		poCtrl1.setOfficeToolbars(readonly);
		poCtrl1.setMenubar(readonly);
		// 设置保存文档的服务器页面
		poCtrl1.setWriter(doc);
		Object owner = org.apache.shiro.SecurityUtils.getSubject();

		poCtrl1.webOpen(basePath + openUrl, OpenModeType.docReadOnly,
				owner.toString());

		poCtrl1.setTagId("PageOfficeCtrl1"); // 此行必须
		return "preview";
	}

	

}